<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>子传父</title>
</head>
<body>
    <!-- 父组件模板 -->
    <div id="app">
        <cpn @itemclick='cpnclick'></cpn>
    </div>
    <!-- 子组件模板 -->
    <template id="cpn">
        <div>
            <button v-for='item in list' @click='btn(item)'>
                {{item.name}}
            </button>
        </div>
    </template>
</body>
<script src='../js/vue.js'></script>
<script>
    // 子组件
    const cpn = {
        template:'#cpn',
        data() {
            return {
                list:[
                    {id:1,name:'热门推荐'},
                    {id:2,name:'手机数码'},
                    {id:3,name:'家用家电'},
                    {id:4,name:'电脑办公'}
                ]
            }
        },
        methods: {
            btn(item){
                // 当子组件需要向父组件传递数据时，需要自定义事件
                this.$emit('itemclick',item)
            }
        },

    }
    // 父组件
    let app = new Vue({
        el: '#app',
        data: {
             msg:'hello vue.js'
        },
        components:{
            cpn
        },
        methods:{
            cpnclick(item){
                console.log(item);
                
            }
        }
    })
</script>
</html>